#include "KamaCoder.h"
using namespace std;

// 深度优先搜索函数，用于遍历网格
void KamaCoder104::dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited,
                       int x, int y, int mark) {
  // 如果该点已经遍历过，则直接返回
  if (visited[x][y] == true) return;

  visited[x][y] = true;  // 标记该点已经遍历过
  grid[x][y] = mark;     // 标记该点的编号
  count++;               // 计数器加一
  // 遍历四个方向
  for (int i = 0; i < 4; i++) {
    int nextx = x + dir[i][0];
    int nexty = y + dir[i][1];
    // 越界检查
    if (nextx < 0 || nexty < 0 || nextx >= grid.size() ||
        nexty >= grid[0].size())
      continue;
    if (grid[nextx][nexty]) {
      dfs(grid, visited, nextx, nexty, mark);
    }
  }
}

// 主函数，用于处理输入和输出结果
int KamaCoder104::run() {
  cin >> n >> m;
  vector<vector<int>> grid(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> grid[i][j];
    }
  }
  vector<vector<bool>> visited(n, vector<bool>(m, false));
  unordered_map<int, int> gridNum;
  int mark = 2;
  bool isAllGrid = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (grid[i][j]) isAllGrid = false;
      if (!visited[i][j] && grid[i][j] == 1) {
        count = 0;
        dfs(grid, visited, i, j, mark);
        gridNum[mark] = count;
        mark++;
      }
    }
  }
  if (isAllGrid) {
    cout << n * m << endl;
    return 0;
  }

  int result = 0;
  unordered_set<int> visitedGrid;  // 标记访问过的岛屿
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      count = 1;            // 计数器
      visitedGrid.clear();  // 每次使用时，清空
      if (grid[i][j] == 0) {
        for (int k = 0; k < 4; k++) {
          int nextx = i + dir[k][0];
          int nexty = j + dir[k][1];
          if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue;

          if (visitedGrid.count(grid[nextx][nexty]))
            continue;  // 添加过的岛屿不再访问
          // 把相邻四面的岛屿数量加起来
          count += gridNum[grid[nextx][nexty]];
          visitedGrid.insert(grid[nextx][nexty]);  // 标记访问过的岛屿
        }
      }
      result = max(result, count);
    }
  }
  cout << result << endl;  // 输出最大的岛屿数量
  return 0;
}